package com.shelpe.services.tenant.item;

import java.util.Iterator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.shelpe.services.tenant.mongomodel.SimbadTask;
import com.shelpe.services.tenant.mongorepository.SimbadTaskRepository;


@Component("taskReader")
public class TaskReader implements ItemReader<SimbadTask>{
	
	private static final Logger logger = LoggerFactory.getLogger(TaskReader.class);
	
	@Autowired
	private SimbadTaskRepository taskRep;
	
	private Iterator<SimbadTask> task;

	@Override
	public SimbadTask read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
		// TODO Auto-generated method stub
		if(task == null){
			Iterable<SimbadTask> it = taskRep.findAll();
			task = it.iterator();
		}
		
		if(task != null && task.hasNext()){
			SimbadTask item = task.next();

			return item;
		}else{
			task = null;//结束
		}
		
		return null;
	}

}
